package com.iotechn.unimall.app.api.card;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.iotechn.unimall.core.exception.*;
import com.iotechn.unimall.core.notify.SMSClient;
import com.iotechn.unimall.core.util.DateUtils;
import com.iotechn.unimall.core.util.GeneratorUtil;
import com.iotechn.unimall.core.util.MoneyUtils;
import com.iotechn.unimall.data.component.LockComponent;
import com.iotechn.unimall.data.domain.*;
import com.iotechn.unimall.data.dto.CardDTO;
import com.iotechn.unimall.data.dto.UserCardDTO;
import com.iotechn.unimall.data.dto.UserDTO;
import com.iotechn.unimall.data.enums.*;
import com.iotechn.unimall.data.mapper.*;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserCardServiceImpl implements UserCardService {

    private static final Logger logger = LoggerFactory.getLogger(UserCardServiceImpl.class);

    @Autowired
    private LockComponent lockComponent;

    @Autowired
    private CardOrderMapper cardOrderMapper;

    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private UserCardMapper userCardMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CardOrderDetailMapper cardOrderDetailMapper;

    @Value("${com.iotechn.unimall.machine-no}")
    private String MACHINE_NO;

    @Value("${com.iotechn.unimall.env}")
    private String ENV;

    @Override
    public Page<UserCardDTO> userCardList(String cardNo,Integer pageNo, Integer limit, Long userId) throws ServiceException {
        List<UserCardDTO> userCardDTOList = new ArrayList<UserCardDTO>();
        EntityWrapper<UserCardDO> wrapper = new EntityWrapper<>();
        if (!StringUtils.isEmpty(cardNo)) {
            wrapper.eq("card_no", cardNo);
        }
        Integer count = userCardMapper.selectCount(wrapper);
        List<UserCardDO> userCardDOList = userCardMapper.selectList(wrapper);
        //循环赋值
        for (UserCardDO userCardDO : userCardDOList) {
            UserCardDTO userCardDTO = new UserCardDTO();
            if (userCardDO == null) continue;
            if (userCardDO.getUserId() != null && userCardDO.getUserId() > 0) {
                UserDO userDO = userMapper.selectById(userCardDO.getUserId());
                if (userDO != null) {
                    userCardDTO.setUserName(!StringUtils.isEmpty(userDO.getNickname()) ? userDO.getNickname() : "");
                    userCardDTO.setPhone(!StringUtils.isEmpty(userDO.getPhone()) ? userDO.getPhone() : "");
                }
            }
            BeanUtils.copyProperties(userCardDO, userCardDTO);
            userCardDTOList.add(userCardDTO);
        }
        return new Page<UserCardDTO>(userCardDTOList, pageNo, limit, count);
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean cardBindUser(String cardNo, String cardSecret, String phone, Long userId) throws ServiceException {
        UserDTO user = SessionUtil.getUser();
        List<UserCardDO> userCardDOS = userCardMapper.selectList(new EntityWrapper<UserCardDO>().eq("card_no", cardNo));
        if(userCardDOS == null || userCardDOS.size() < 1){
            throw new AdminServiceException(new ServiceExceptionDefinition(500, "绑定的会员卡号不存在"));
        }
        UserCardDO userCardDO = userCardDOS.get(0);
        //查询当前卡号 是已销售卡
        Integer integer = cardOrderMapper.checkCardIsShop(userCardDO.getCardNo());
        if(integer.intValue() < 1 || userCardDO.getStatus().intValue() == CardUseType.LOCK.getCode()){
            throw new AdminServiceException(new ServiceExceptionDefinition(500, "此卡未销售,不可绑定用户"));
        }
        if(userCardDO.getStatus().intValue() == CardUseType.INVALID.getCode()){
            throw new AdminServiceException(new ServiceExceptionDefinition(500, "此卡已绑定"));
        }
        //校验卡密
        if(!cardSecret.equals(userCardDO.getCardSecret())){
            throw new AdminServiceException(new ServiceExceptionDefinition(500, "卡密错误"));
        }
        return true;
    }
}
